package com.wowangz.cms.syscore.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class CostUtils {

	public static BigDecimal formatComma2BigDecimal(Object obj) {
		String val = String.valueOf(obj);
		if (val == null)
			return new BigDecimal("0.00");
		val = val.replaceAll(",", "");
		if (!isNumber(val))
			return new BigDecimal("0.00");
		BigDecimal decimal = new BigDecimal(val);
		decimal = decimal.setScale(2, RoundingMode.HALF_UP);

		return decimal;
	}

	private static boolean isDouble(String value) {
		try {
			Double.parseDouble(value);
			if (value.contains("."))
				return true;
			return false;
		} catch (NumberFormatException e) {
			return false;
		}
	}

	private static boolean isNumber(String value) {
		return isInteger(value) || isDouble(value);
	}

	private static boolean isInteger(String value) {
		try {
			Integer.parseInt(value);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}
}
